package com.example.a13001.shoppingmalltemplate.presenter;

import android.content.Context;
import android.content.Intent;

import com.example.a13001.shoppingmalltemplate.manager.DataManager;
import com.example.a13001.shoppingmalltemplate.modle.Address;
import com.example.a13001.shoppingmalltemplate.modle.CloaseAccount;
import com.example.a13001.shoppingmalltemplate.modle.CommonResult;
import com.example.a13001.shoppingmalltemplate.modle.LoginStatus;
import com.example.a13001.shoppingmalltemplate.modle.ShopCarGoods;
import com.example.a13001.shoppingmalltemplate.mvpview.AdressManagerView;
import com.example.a13001.shoppingmalltemplate.mvpview.ShopCarView;
import com.example.a13001.shoppingmalltemplate.mvpview.View;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class AdressManagerPredenter implements Presenter{
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private Context mContext;
    private AdressManagerView mAdressManagerView;
    private Address mAddress;
    private CommonResult mCommonResult;



    public AdressManagerPredenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCreate() {
        mCompositeSubscription=new CompositeSubscription();
        manager=new DataManager(mContext);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(View view) {
        mAdressManagerView=(AdressManagerView) view;
    }

    @Override
    public void attachIncomingIntent(Intent intent) {

    }

    /**
     *  获取会员收货地址
     * @param companyid   站点ID
     * @param code   安全校验码
     * @param timestamp  时间戳
     * @param pagesize   每页显示数量
     * @param pageindex   当前页数
     * @param addressid   收货地址ID
     * @param main   为1时加载默认收货地址，其它调用所有
     * @return
     */
    public void getAdressList(String companyid,String code,String timestamp,int pagesize,int pageindex,int main,String addressid) {

        mCompositeSubscription.add(manager.getAddressList(companyid,code,timestamp,pagesize,pageindex,main,addressid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Address>() {
                    @Override
                    public void onCompleted() {
                        if (mAdressManagerView!=null){
                            mAdressManagerView.onSuccess(mAddress);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mAdressManagerView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(Address address) {
                        mAddress=address;
                    }
                }));
    }
    /**
     * 删除会员收货地址
     * @param companyid   站点ID
     * @param code      安全校验码
     * @param timestamp  时间戳
     * @param addressid   收货地址ID
     * @return
     */
    public void deleteAddress(String companyid,String code,String timestamp,String addressid) {

        mCompositeSubscription.add(manager.deleteAddress(companyid,code,timestamp,addressid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {
                        if (mAdressManagerView!=null){
                            mAdressManagerView.onSuccessDeleteAddress(mCommonResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mAdressManagerView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        mCommonResult=commonResult;
                    }
                }));
    }
    /**
     * 设置默认会员收货地址
     * @param companyid   站点ID
     * @param code        安全校验码
     * @param timestamp   时间戳
     * @param addressid   收货地址ID
     * @return
     */
    public void setDefaultAddress(String companyid,String code,String timestamp,String addressid) {

        mCompositeSubscription.add(manager.setDefaultAddress(companyid,code,timestamp,addressid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {
                        if (mAdressManagerView!=null){
                            mAdressManagerView.onSuccessSetDefaultAddress(mCommonResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mAdressManagerView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        mCommonResult=commonResult;
                    }
                }));
    }
}
